﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Net
{
    using System;

    using Mews.Net.Nntp;

    /// <summary>Provides helper methods to execute the same NNTP command multiple times with different responses.
    /// </summary>
    /// <typeparam name="TResult">The type of the result that the executed method will produce.</typeparam>
    internal sealed class ExecuteHelper<TResult> : ExecuteHelperBase<TResult>
    {
        internal ExecuteHelper(FakeServer server, Func<TResult> method, string expectedCommand) :
            base(server, method, expectedCommand)
        {
        }

        internal void AssertThrow(int responseCode)
        {
            switch (responseCode)
            {
                case 400:
                    AssertThrow<ServiceUnavailableException>("400 Service not available\r\n");
                    break;
                case 403:
                    AssertThrow<ServerFaultException>("403 Internal fault\r\n");
                    break;
                case 411:
                    AssertThrow<NoSuchGroupException>("411 No such newsgroup\r\n");
                    break;
                case 412:
                    AssertThrow<NoGroupSelectedException>("412 No newsgroup selected\r\n");
                    break;
                case 420:
                    AssertThrow<InvalidCurrentArticleNumberException>("420 Current article number is invalid\r\n");
                    break;
                case 421:
                    AssertThrow<NoNextArticleException>("421 No next article in this group\r\n");
                    break;
                case 422:
                    AssertThrow<NoPreviousArticleException>("422 No previous article in this group\r\n");
                    break;
                case 423:
                    AssertThrow<NoSuchArticleException>("423 No article with that number or in that range\r\n");
                    break;
                case 430:
                    AssertThrow<NoSuchArticleException>("430 No article with that message-id\r\n");
                    break;
                case 440:
                    AssertThrow<PostingNotPermittedException>("440 Posting not permitted\r\n");
                    break;
                case 441:
                    AssertThrow<PostingFailedException>("441 Posting failed\r\n");
                    break;
                case 480:
                    AssertThrow<MissingAuthenticationException>(
                        "480 Command unavailable until the client has authenticated itself\r\n");
                    break;
                case 481:
                    AssertThrow<AuthenticationFailedException>(
                        "481 Authentication failed\r\n");
                    break;
                case 483:
                    AssertThrow<MissingPrivacyException>(
                        "483 Command unavailable until suitable privacy has been arranged\r\n");
                    break;
                case 500:
                    AssertThrow<UnknownCommandException>("500 Unknown command\r\n");
                    break;
                case 502:
                    AssertThrow<CommandNotPermittedException>(
                        "502 Service permanently unavailable or command not permitted\r\n");
                    break;
                case 503:
                    AssertThrow<FeatureNotSupportedException>("503 Feature not supported");
                    break;
                case 999:
                    AssertThrow<UnexpectedResponseException>("999 whatever\r\n");
                    break;
                default:
                    throw new ArgumentException("Unknown error response code: " + responseCode);
            }
        }
    }
}
